//
//  ZJAppUpdateAlertView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/7/13.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJAppUpdateAlertView.h"

@interface ZJAppUpdateAlertView ()
/** 背景白图片 */
@property (nonatomic, strong) UIView *bgView;

/** title */
@property (nonatomic, strong) UILabel *titleLabel;
/** 更新内容 */
@property (nonatomic, strong) UITextView *textView;
/** 横向分割线 */
@property (nonatomic, strong) UILabel *spaceline;
/** 竖向分割线 */
@property (nonatomic, strong) UILabel *vspaceline;
/** 取消按钮 */
@property (nonatomic, strong) UIButton *cancleButton;
/** 确认按钮 */
@property (nonatomic, strong) UIButton *affirmButton;
@end

@implementation ZJAppUpdateAlertView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.titleLabel];
        [self.bgView addSubview:self.spaceline];
        [self.bgView addSubview:self.vspaceline];
        [self.bgView addSubview:self.cancleButton];
        [self.bgView addSubview:self.textView];
        [self.bgView addSubview:self.affirmButton];
        
        self.bgView.sd_layout
        .leftSpaceToView(self, 40 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self, 40 * [UIScreen zj_screenWScale])
        .centerXIs(self.centerX)
        .centerYIs(self.centerY);
        
        self.bgView.sd_cornerRadius = @(10 * [UIScreen zj_screenWScale]);
        
        self.cancleButton.sd_layout
        .bottomSpaceToView(self.bgView, 0)
        .leftSpaceToView(self.bgView,0)
        .heightIs(45 * [UIScreen zj_screenHScale])
        .widthRatioToView(self.bgView, 0.5);
        
        
        
        self.affirmButton.sd_layout
        .bottomEqualToView(self.cancleButton)
        .rightSpaceToView(self.bgView, 0)
        .heightRatioToView(self.cancleButton, 1)
        .widthRatioToView(self.cancleButton, 1);
        
        self.vspaceline.sd_layout
        .topEqualToView(self.affirmButton)
        .leftSpaceToView(self.cancleButton, - 0.5)
        .widthIs(1)
        .heightRatioToView(self.cancleButton, 1);
        
        self.spaceline.sd_layout
        .bottomSpaceToView(self.cancleButton,0)
        .leftSpaceToView(self.bgView, 0)
        .rightSpaceToView(self.bgView, 1)
        .heightIs(1);
        
        self.titleLabel.sd_layout
        .topSpaceToView(self.bgView, 12 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.spaceline)
        .rightEqualToView(self.spaceline)
        .heightIs([NSString heightWithTextFont:self.titleLabel.font]);
        
        self.textView.sd_layout
        .topSpaceToView(self.titleLabel, 10 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.bgView, 20 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.bgView, 20 * [UIScreen zj_screenWScale])
        .heightIs(10);
    }
    return self;
}



-(void)textViewDidChange:(UITextView *)textView{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 8 * [UIScreen zj_screenHScale];// 字体的行间距
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:textView.font,
                                 NSForegroundColorAttributeName:[UIColor zj_colorWithHexString:@"626262"],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    
    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
    
    
    CGFloat maxHeight = 126 * [UIScreen zj_screenHScale];
    CGRect frame = textView.frame;
    CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
    CGSize size = [textView sizeThatFits:constraintSize];
    if (size.height <= frame.size.height) {
        size.height=frame.size.height;
    }else{
        if (size.height >= maxHeight)
        {
            size.height = maxHeight;
            textView.scrollEnabled = YES;   // 允许滚动
        }
        else
        {
            textView.scrollEnabled = NO;    // 不允许滚动
        }
    }
    self.textView.sd_layout
    .topSpaceToView(self.titleLabel, 10 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.bgView, 20 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.bgView, 20 * [UIScreen zj_screenWScale])
    .heightIs(size.height);
    
    [self.textView updateLayout];
}


#pragma mark - 点击事件
- (void)action_canleButton{
    if (self.delegate) {
        [self.delegate ZJAppUpdateAlertViewDelegate_canleClick];
    }
}
- (void)action_affirmButton{
    if (self.delegate) {
        [self.delegate ZJAppUpdateAlertViewDelegate_updateClick];
    }
}

#pragma mark - Setter
- (void)setDict:(NSDictionary *)dict{
    /** 
     desc = "";
     path = "http://117.184.59.230:999/iPB/downLoad.html";
     state = 2;
     type = 1;
     versionCode = "1.01";
     versionName = 101;
     */
    _dict = dict;
    //判断是不是强制更新
    if ([dict[@"state"] isEqualToString:@"0"]) {//不是强制更新
        
    }else{//强制更新
        self.cancleButton.hidden = YES;
        self.vspaceline.hidden = YES;
        self.affirmButton.sd_layout
        .bottomSpaceToView(self.bgView, 0)
        .rightSpaceToView(self.bgView, 0)
        .heightRatioToView(self.cancleButton, 1)
        .leftSpaceToView(self.bgView, 0);
        
        [self.affirmButton updateLayout];
    }
    //
    NSString *desc = [dict objectForKey:@"desc"];
    if (desc.length > 0) {
        self.textView.text = desc;
    }else{
        self.textView.text = @"是否立即更新";
        self.textView.textAlignment = NSTextAlignmentCenter;
    }
    
    [self textViewDidChange:self.textView];
    
    self.bgView.sd_layout
    .leftSpaceToView(self, 40 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self, 40 * [UIScreen zj_screenWScale])
    .centerXIs(self.centerX)
    .centerYIs(self.centerY)
    .heightIs((45 + 10 * 2  + 12)* [UIScreen zj_screenHScale] + self.textView.height + self.titleLabel.height + 1);
    
    [self.bgView updateLayout];
}


#pragma mark - Getters

- (UIView *)bgView{
    if (_bgView == nil){
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 8 * [UIScreen zj_screenWScale];
    }
    return _bgView;
}

- (UILabel *)titleLabel{

    if (_titleLabel == nil){
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor textBlackColor];
        _titleLabel.font = [UIFont systemFontOfSize:16 * [UIScreen zj_textFontScale]];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"有新版本更新";
    }
    return _titleLabel;
}

- (UILabel *)spaceline{
    if (_spaceline == nil){
        _spaceline = [UILabel new];
        _spaceline.backgroundColor = [UIColor viewBGGrayColor];
    }
    return _spaceline;
}
- (UILabel *)vspaceline{
    if (_vspaceline == nil){
        _vspaceline = [UILabel new];
        _vspaceline.backgroundColor = [UIColor viewBGGrayColor];
    }
    return _vspaceline;
}

- (UIButton *)cancleButton{
    if (_cancleButton == nil){
        _cancleButton = [UIButton new];
        [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancleButton setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
        _cancleButton.titleLabel.font = [UIFont titleTextFont];
        [_cancleButton addTarget:self action:@selector(action_canleButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _cancleButton;
}

- (UIButton *)affirmButton{
    if (_affirmButton == nil){
        _affirmButton = [UIButton new];
        [_affirmButton setTitle:@"更新" forState:UIControlStateNormal];
        [_affirmButton setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
        _affirmButton.titleLabel.font = [UIFont titleTextFont];
        [_affirmButton addTarget:self action:@selector(action_affirmButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _affirmButton;
}
- (UITextView *)textView{
    if (_textView == nil){
        _textView = [UITextView new];
//        _textView.textColor = [UIColor textBlackColor];
        _textView.font = [UIFont subTitleTextFont];
        _textView.editable = NO;
    }
    return _textView;
}

@end
